package com.htx.zqs.blesmartmask.utils;

import android.util.Log;

import com.htx.zqs.blesmartmask.listeners.BmobListener;
import com.htx.zqs.blesmartmask.bean.Numb;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by zhao on 2018/6/3.
 */

public class BmobUtil {
    BmobListener bmobListener;

    public BmobUtil(BmobListener bmobListener) {
        this.bmobListener = bmobListener;
    }

    /**
     * @param address 获取数据
     */
    public void getBLEDataFromBmob(String address, final int code) {
        BmobQuery<Numb> query = new BmobQuery<Numb>();
        query.addWhereEqualTo("address", address);
        query.findObjects(new FindListener<Numb>() {
            @Override
            public void done(final List<Numb> list, final BmobException e) {
                if (e == null) {

                    Log.e("查询", "done: " + list);
                    ThreadUtil.executeMainThread(new Runnable() {
                        @Override
                        public void run() {
                            if (bmobListener != null) {
                                bmobListener.onSuccess(code, list);
                            }
                        }
                    });
                } else {
                    String localizedMessage = e.getLocalizedMessage();

                    Log.e("device", "done: " + localizedMessage);
                    ThreadUtil.executeMainThread(new Runnable() {
                        @Override
                        public void run() {
                            bmobListener.onError(code, e);
                        }
                    });
                }
            }
        });
    }


    /**
     * @param objectId 更新
     * @param count
     */
    public void upData(String objectId, Integer count, final int code) {
        Numb numb = new Numb();
        numb.setNumberlong(count);
        numb.update(objectId, new UpdateListener() {
            @Override
            public void done(final BmobException e) {
                if (e == null) {

                    Log.e("limit", "更新成功");
                    ThreadUtil.executeMainThread(new Runnable() {
                        @Override
                        public void run() {
                            bmobListener.onSuccess(code, null);
                        }
                    });
                } else {

                    Log.e("limit", "更新失败");
                    Log.i("bmob", "更新失败：" + e.getMessage() + "," + e.getErrorCode());
                    ThreadUtil.executeMainThread(new Runnable() {
                        @Override
                        public void run() {
                            bmobListener.onError(code, e);
                        }
                    });
                }
            }
        });
    }

    /**
     * @param address 保存
     * @param count
     */
    public void saveData(String address, Integer count, String system, final int code) {
        final Numb numb = new Numb();
        numb.setAddress(address);
        numb.setNumberlong(count);
        numb.setSystem(system);
        numb.save(new SaveListener<String>() {
            @Override
            public void done(String objectId, final BmobException e) {
                if (e == null) {
                    //toast("创建数据成功：" + objectId);
                    Log.e("limit", "保存成功");
                    ThreadUtil.executeMainThread(new Runnable() {
                        @Override
                        public void run() {
                            bmobListener.onSuccess(code, null);
                        }
                    });

                } else {
                    Log.e("limit", "保存失败");
                    Log.i("bmob", "失败：" + e.getMessage() + "," + e.getErrorCode());
                    ThreadUtil.executeMainThread(new Runnable() {
                        @Override
                        public void run() {
                            bmobListener.onError(code, e);
                        }
                    });
                }
            }
        });
    }




}
